// Copyright 2010, Vanya Davidenko.
// Используемая кодировка: UTF-8

#ifndef SCRW_TESTING_URL_CONVERTER_TESTING_H_
#define SCRW_TESTING_URL_CONVERTER_TESTING_H_

#include <cstring>
#include <gtest/gtest.h>
#include "url_converter.h"


namespace scrw
{

namespace testing
{

class UrlConverterTesting : public ::testing::Test
{
  public:
    virtual void SetUp()
    {
    }


    virtual void TearDown()
    {

    }

    UnicodeUrl U(const char* str) const
    {
        // 3 параметр должен совпадать с кодировкой файла!
        return UnicodeUrl(str, I32(::std::strlen(str)), "utf-8");
    }

    AsciiUrl A(const char* str) const
    {
        return AsciiUrl(str);
    }


    UrlConverter c;
};


TEST_F(UrlConverterTesting, Errors)
{
    EXPECT_THROW(c.Unicode2Ascii(U("http://")),
                 libv::PrecondException);
    EXPECT_THROW(c.Unicode2Ascii(U("/root/dir")),
                 libv::PrecondException);
    EXPECT_THROW(c.Unicode2Ascii(U("www.ya.ru")),
                 libv::PrecondException);
    EXPECT_THROW(c.Unicode2Ascii(U("mailto:a@ya.ru")),
                 libv::PrecondException);
    EXPECT_THROW(c.Unicode2Ascii(U("хттп://я.рф")),
                 libv::PrecondException);

    EXPECT_THROW(c.Ascii2Unicode(A("http://")),
                 libv::PrecondException);
    EXPECT_THROW(c.Ascii2Unicode(A("/root/dir")),
                 libv::PrecondException);
    EXPECT_THROW(c.Ascii2Unicode(A("www.ya.ru")),
                 libv::PrecondException);
    EXPECT_THROW(c.Ascii2Unicode(A("mailto:a@ya.ru")),
                 libv::PrecondException);
    EXPECT_THROW(c.Ascii2Unicode(A("хттп://я.рф")),
                 libv::PrecondException);

    EXPECT_THROW(c.Ascii2Unicode(A("http://xn--o1a.xn--p1ai/%")),
                 UrlConverterException);
    EXPECT_THROW(c.Ascii2Unicode(A("http://xn--o1a.xn--p1ai/%2")),
                 UrlConverterException);
    EXPECT_THROW(c.Ascii2Unicode(A("http://xn--o1a.xn--p1ai/%2Z")),
                 UrlConverterException);

}


TEST_F(UrlConverterTesting, Domain)
{
    EXPECT_STREQ("http://ya.ru",
                 c.Unicode2Ascii(U("http://ya.ru")).c_str());
    EXPECT_STREQ("ftp://ya.ru/",
                 c.Unicode2Ascii(U("ftp://ya.ru/")).c_str());
    EXPECT_STREQ("http://xn--d1abbgf6aiiy.xn--p1ai",
                 c.Unicode2Ascii(U("http://президент.рф")).c_str());
    EXPECT_STREQ("http://xn--jdk.xn--kdk",  // Код длиннее, чем у русского.
                 c.Unicode2Ascii(U("http://ヌ.ネ")).c_str());

    EXPECT_STREQ("http://xn--jdk.xn--kdk",  // Повторное кодирование не изменяет
                 c.Unicode2Ascii(U("http://xn--jdk.xn--kdk")).c_str());


    EXPECT_EQ(1, U("http://ya.ru") ==
                 c.Ascii2Unicode(A("http://ya.ru")));
    EXPECT_EQ(1, U("ftp://ya.ru/") ==
                 c.Ascii2Unicode(A("ftp://ya.ru/")));
    EXPECT_EQ(1, U("http://президент.рф") ==
                 c.Ascii2Unicode(A("http://xn--d1abbgf6aiiy.xn--p1ai")));
    EXPECT_EQ(1, U("http://ヌ.ネ") ==
                 c.Ascii2Unicode(A("http://xn--jdk.xn--kdk")));

    EXPECT_EQ(1, U("http://ヌ.ネ") ==  // Повторное декодирование не изменяет
                 c.Ascii2Unicode(U("http://ヌ.ネ")));
}


TEST_F(UrlConverterTesting, PercentEncoding)
{
    EXPECT_STREQ("http://ya.ru/market",
                 c.Unicode2Ascii(U("http://ya.ru/market")).c_str());

    EXPECT_STRCASEEQ("http://ya.ru/%d0%bc%d0%b0%d1%80%d0%ba%d0%b5%d1%82",
                     c.Unicode2Ascii(U("http://ya.ru/маркет")).c_str());

    EXPECT_STREQ("http://ya.ru/;/?:@&=+$,",
                 c.Unicode2Ascii(U("http://ya.ru/;/?:@&=+$,")).c_str());

    EXPECT_STREQ("http://ya.ru/;/aAbBzZ09-_.!~*'()",
                 c.Unicode2Ascii(
                      U("http://ya.ru/;/aAbBzZ09-_.!~*'()")).c_str());

    EXPECT_STRCASEEQ("http://ya.ru/%d0%b0%d0%90%d0%91%d1%8f%d0%af%e3%83%8d",
                     c.Unicode2Ascii(U("http://ya.ru/аАБяЯネ")).c_str());


    EXPECT_EQ(1, U("http://ya.ru/market") ==
                 c.Ascii2Unicode(A("http://ya.ru/market")));

    EXPECT_EQ(1, U("http://ya.ru/мар") ==
                 c.Ascii2Unicode(A("http://ya.ru/%d0%bc%d0%b0%d1%80")));

    EXPECT_EQ(1, U("http://ya.ru/;/?:@&=+$,") ==
                 c.Ascii2Unicode(A("http://ya.ru/;/?:@&=+$,")));

    EXPECT_EQ(1, U("http://ya.ru/;/aAbBzZ09-_.!~*'()") ==
                 c.Ascii2Unicode(A("http://ya.ru/;/aAbBzZ09-_.!~*'()")));

    EXPECT_EQ(1, U("http://y.ru/аАяЯネ")==
                 c.Ascii2Unicode(
                     A("http://y.ru/%d0%b0%d0%90%d1%8f%d0%af%e3%83%8d")));

        EXPECT_EQ(1, U("http://y.ru/?a=1&b=0")==
                 c.Ascii2Unicode(A("http://y.ru/?a=1&amp;b=0")));

    EXPECT_EQ(1, U("http://y.ru/аАяЯネ")==
                 c.Ascii2Unicode(U("http://y.ru/аАяЯネ")));
}


TEST_F(UrlConverterTesting, Combinations)
{
    EXPECT_STREQ("http://local",
                 c.Unicode2Ascii(U("http://local")).c_str());

    EXPECT_STREQ("http://ya.ru:80",
                 c.Unicode2Ascii(U("http://ya.ru:80")).c_str());

    EXPECT_STREQ("http://ya.ru:80/",
                 c.Unicode2Ascii(U("http://ya.ru:80/")).c_str());

    EXPECT_STREQ("http://ya.ru:80/market",
                 c.Unicode2Ascii(U("http://ya.ru:80/market")).c_str());

    EXPECT_STREQ("http://ya.ru:80/market?a=1",
                 c.Unicode2Ascii(
                     U("http://ya.ru:80/market?a=1")).c_str());

    // По стандарту так правильнее , хотя серверы принимают оба варианта.
    EXPECT_STREQ("http://ya.ru:80/market?a=1%23anchor",
                 c.Unicode2Ascii(
                     U("http://ya.ru:80/market?a=1#anchor")).c_str());

    EXPECT_STREQ(
        "http://xn--d1abbgf6aiiy.xn--p1ai:80/%d0%bd%d0%be%d0%b2%d0%be%d1%81%d1"
        "%82%d0%b8?%d0%b0=1%23%d0%bd%d0%b0%d1%87%d0%b0%d0%bb%d0%be",
        c.Unicode2Ascii(
            U("http://президент.рф:80/новости?а=1#начало")).c_str());



    EXPECT_EQ(1, U("http://local") ==
                 c.Ascii2Unicode(A("http://local")));

    EXPECT_EQ(1, U("http://ya.ru:80") ==
                 c.Ascii2Unicode(A("http://ya.ru:80")));

    EXPECT_EQ(1, U("http://ya.ru:80/market") ==
                 c.Ascii2Unicode(A(("http://ya.ru:80/market"))));

    EXPECT_EQ(1, U("http://ya.ru:80/market?a=1") ==
                 c.Ascii2Unicode(A("http://ya.ru:80/market?a=1")));

    EXPECT_EQ(1, U("http://ya.ru:80/market?a=1#anchor") ==
                 c.Ascii2Unicode(
                     A("http://ya.ru:80/market?a=1%23anchor")));

    EXPECT_EQ(1, U("http://президент.рф:80/новости?а=1#начало") ==
                 c.Ascii2Unicode(
                     A("http://xn--d1abbgf6aiiy.xn--p1ai:80/%d0%bd%d0%be"
                     "%d0%b2%d0%be%d1%81%d1%82%d0%b8?%d0%b0=1%23%d0"
                     "%bd%d0%b0%d1%87%d0%b0%d0%bb%d0%be")));

   EXPECT_EQ(1, U("http://президент.рф:80/новости?а=1#начало") ==
                 c.Ascii2Unicode(
                     U("http://президент.рф:80/новости?а=1#начало")));
}

}  // testing

}  // scrw


#endif  // SCRW_TESTING_URL_CONVERTER_TESTING_H_
